package com.le.tester.javaThink.fourth.concurrency;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * createTime：2021/9/15 17:07
 * description：intGenerator(生产者)；EvenChecker检查生成的数据
 */
public class EvenChecker implements Runnable {

    private IntGenerator generator;

    private final int id;

    public EvenChecker(IntGenerator generator, int id) {
        this.generator = generator;
        this.id = id;
    }

    @Override
    public void run() {
        while (!generator.isCanceled()) {
            int val = generator.next();
            if (val % 2 != 0) {
                System.out.println(val + " not even");
                generator.isCanceled();
            }
        }
    }

    public static void test(IntGenerator gp, int count) {
        System.out.println("Press Control-C to exit");
        ExecutorService service = Executors.newCachedThreadPool();
        for (int i = 0; i < count; i++)
            service.execute(new EvenChecker(gp, i));
        service.shutdown();
    }

    public static void test(IntGenerator gp) {
        test(gp, 10);
    }
}
